<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="公司名称" prop="factoryName">
        <el-input
          v-model="queryParams.factoryName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户类别" prop="clientCategory">
        <el-select v-model="queryParams.clientCategory" placeholder="请选择客户类别" clearable>
          <el-option
            v-for="dict in dict.type.customer_cate"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>

      </el-form-item>
      <el-form-item label="客户类型" prop="clientType">
        <el-select v-model="queryParams.clientType" placeholder="请选择客户类型" clearable>
          <el-option
            v-for="dict in dict.type.cus_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户名称" prop="clientName">
        <el-input
          v-model="queryParams.clientName"
          placeholder="请输入客户地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户联系人" prop="clientLinker">
        <el-input
          v-model="queryParams.clientLinker"
          placeholder="请输入客户联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户电话" prop="clientPhone">
        <el-input
          v-model="queryParams.clientPhone"
          placeholder="请输入客户电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createUser">
        <el-input
          v-model="queryParams.createUser"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['sales:files:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="addRow"
          v-hasPermi="['sales:files:add']"
        >添加行</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sales:files:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sales:files:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sales:files:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table ref="demoTable" highlight-current-row v-loading="loading" :data="filesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="公司名称" width="110" align="center" prop="factoryName"/>
      <el-table-column label="公司代码" width="110" align="center" prop="factoryCode"/>
      <el-table-column label="客户代码" width="110" align="center" prop="clientCode">
        <template slot-scope="scope">
          <el-input v-model="scope.row.clientCode"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="客户类别" width="110" align="center" prop="clientCategory">
        <template slot-scope="scope">
          <el-select v-model="scope.row.clientCategory" placeholder="请选择客户类别" clearable>
            <el-option
              v-for="dict in dict.type.customer_cate"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="客户类型" width="110" align="center" prop="clientType">
        <template slot-scope="scope">
          <el-select v-model="scope.row.clientType" placeholder="请选择客户类型" clearable>
            <el-option
              v-for="dict in dict.type.cus_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="客户名称" width="110" align="center" prop="clientName">
        <template slot-scope="scope">
          <el-input v-model="scope.row.clientName">
          </el-input>
        </template>
      </el-table-column>
      <el-table-column label="客户简称" width="110" align="center" prop="shortName">
        <template slot-scope="scope">
          <el-input v-model="scope.row.shortName">
          </el-input>
        </template>
      </el-table-column>
      <el-table-column label="统一社会信用代码" width="140" align="center" prop="creditCode">
        <template slot-scope="scope">
          <el-input v-model="scope.row.creditCode">
          </el-input>
        </template>
      </el-table-column>
      <el-table-column label="客户地址" width="110" align="center" prop="clientAdress">
        <template slot-scope="scope">
          <el-input v-model="scope.row.clientAdress">
          </el-input>
        </template>
      </el-table-column>
      <el-table-column label="客户联系人" width="110" align="center" prop="clientLinker">
        <template slot-scope="scope">
          <el-input v-model="scope.row.clientLinker">
          </el-input>
        </template>
      </el-table-column>
      <el-table-column label="客户电话" width="110" align="center" prop="clientPhone">
        <template slot-scope="scope">
          <el-input v-model="scope.row.clientPhone">
          </el-input>
        </template>
      </el-table-column>
      <el-table-column label="客户税号" width="110" align="center" prop="clientEin">
        <template slot-scope="scope">
          <el-input v-model="scope.row.clientEin">
          </el-input>
        </template>
      </el-table-column>
      <el-table-column label="客户开户行" width="110" align="center" prop="clientBank">
        <template slot-scope="scope">
          <el-input v-model="scope.row.clientBank">
          </el-input>
        </template>
      </el-table-column>
      <el-table-column label="客户账号" width="110" align="center" prop="clientAcc">
        <template slot-scope="scope">
          <el-input v-model="scope.row.clientAcc">
          </el-input>
        </template>
      </el-table-column>
      <el-table-column label="邮编" width="110" align="center" prop="postcode">
        <template slot-scope="scope">
          <el-input v-model="scope.row.postcode">
          </el-input>
        </template>
      </el-table-column>
      <el-table-column label="客户服务人员" width="110" align="center" prop="cliSerPerson">
        <template slot-scope="scope">
          <el-input v-model="scope.row.cliSerPerson">
          </el-input>
        </template>
      </el-table-column>
      <el-table-column label="建档日期" align="center" prop="filingDate" width="180">
        <template slot-scope="scope">
          <el-date-picker clearable
                          v-model="scope.row.filingDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择建档日期">
          </el-date-picker>
<!--          <span>{{ parseTime(scope.row.filingDate, '{y}-{m}-{d}') }}</span>-->
        </template>
      </el-table-column>
      <el-table-column label="经办人" width="110" align="center" prop="agent">
        <template slot-scope="scope">
          <el-input v-model="scope.row.agent">
          </el-input>
        </template>
      </el-table-column>
      <el-table-column label="创建人" width="110" align="center" prop="createUser"/>
      <el-table-column label="创建时间" width="110" align="center" prop="createTime">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="150" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="bcRow(scope.row)"
            v-hasPermi="['sales:files:edit']"
          >保存</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sales:files:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户档案对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公司代码" prop="factoryCode">
          <el-input v-model="form.factoryCode" placeholder="请输入公司代码" />
        </el-form-item>
        <el-form-item label="公司名称" prop="factoryName">
          <el-input v-model="form.factoryName" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="客户类别" prop="clientCategory">
          <el-input v-model="form.clientCategory" placeholder="请输入客户类别" />
        </el-form-item>
        <el-form-item label="客户代码" prop="clientCode">
          <el-input v-model="form.clientCode" placeholder="请输入客户代码" />
        </el-form-item>
        <el-form-item label="客户名称" prop="clientName">
          <el-input v-model="form.clientName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="客户地址" prop="clientAdress">
          <el-input v-model="form.clientAdress" placeholder="请输入客户地址" />
        </el-form-item>
        <el-form-item label="客户联系人" prop="clientLinker">
          <el-input v-model="form.clientLinker" placeholder="请输入客户联系人" />
        </el-form-item>
        <el-form-item label="客户电话" prop="clientPhone">
          <el-input v-model="form.clientPhone" placeholder="请输入客户电话" />
        </el-form-item>
        <el-form-item label="客户税号" prop="clientEin">
          <el-input v-model="form.clientEin" placeholder="请输入客户税号" />
        </el-form-item>
        <el-form-item label="客户开户行" prop="clientBank">
          <el-input v-model="form.clientBank" placeholder="请输入客户开户行" />
        </el-form-item>
        <el-form-item label="客户账号" prop="clientAcc">
          <el-input v-model="form.clientAcc" placeholder="请输入客户账号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 客户信息导入对话框 -->
    <el-dialog :close-on-click-modal="false" :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!--  <div class="el-upload__tip" slot="tip">
              <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
            </div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listFiles, getFiles, delFiles, addFiles, updateFiles } from "@/api/sales/clientFiles";
  import {getToken} from "@/utils/auth";
  import {listAllOrgManage} from "@/api/hr/orgManage";

  export default {
    name: "Files",
    dicts: ['customer_cate','cus_type'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 客户档案表格数据
        filesList: [],
        gcCodeList:[],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          factoryCode: null,
          factoryName: null,
          clientCategory: null,
          clientType: null,
          clientCode: null,
          clientName: null,
          clientAdress: null,
          clientLinker: null,
          clientPhone: null,
          clientEin: null,
          clientBank: null,
          clientAcc: null,
          cliSerPerson: null,
          filingDate: null,
          cumuMoney: null,
          debtMoney: null,
          schRepayDate: null,
          schRepayMoney: null,
          actualRepayMoney: null,
          agent: null,
          createUser: null
        },
        // 表单参数
        form: {},
        // 客户导入参数
        upload: {
          // 是否显示弹出层（物料导入）
          open: false,
          // 弹出层标题（物料导入）
          title: "导入客户信息",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/sales/files/importData"
        },
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();

      listAllOrgManage(this.queryParams2).then(response => {
        this.gcCodeList = response.rows;
      });
    },
    methods: {
      bcRow(row){
        if (row.id != null) {
          updateFiles(row).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        } else {
          addFiles(row).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      },
      addRow(){
        var d = {
          factoryCode:'',
          factoryName:'',
          // index: this.dataId++,
          // riskSourceName: "",
          // riskLevel: "",
          // riskPointName: "",
          // type: "",
          // hiddenDanger: "",
          // dangerLevel: "",
          // accident: "",
        };
        this.filesList.push(d);
        setTimeout(() => {
          this.$refs.demoTable.setCurrentRow(d);
        }, 10);

        console.log('添加行')
      },
      /** 查询客户档案列表 */
      getList() {
        this.loading = true;
        listFiles(this.queryParams).then(response => {
          this.filesList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          factoryCode: null,
          factoryName: null,
          clientCategory: null,
          clientType: null,
          clientCode: null,
          clientName: null,
          clientAdress: null,
          clientLinker: null,
          clientPhone: null,
          clientEin: null,
          clientBank: null,
          clientAcc: null,
          cliSerPerson: null,
          filingDate: null,
          cumuMoney: null,
          debtMoney: null,
          schRepayDate: null,
          schRepayMoney: null,
          actualRepayMoney: null,
          agent: null,
          createTime: null,
          createUser: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        console.log("diani ")
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加客户档案";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getFiles(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改客户档案";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateFiles(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addFiles(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除客户档案编号为"' + ids + '"的数据项？').then(function() {
          return delFiles(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('sales/files/export', {
          ...this.queryParams
        }, `files_${new Date().getTime()}.xlsx`)
      },
      /** 导入按钮操作 */
      handleImport() {
        this.upload.title = "客户导入";
        this.upload.open = true;
      },
      /** 下载模板操作 */
      importTemplate() {
        this.download('sales/files/importTemplate', {
        }, `客户数据信息.xlsx`)
      },
      // 文件上传中处理
      handleFileUploadProgress(event, file, fileList) {
        this.upload.isUploading = true;
      },
      // 文件上传成功处理
      handleFileSuccess(response, file, fileList) {
        this.upload.open = false;
        this.upload.isUploading = false;
        this.$refs.upload.clearFiles();
        this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
        this.getList();
      },
      // 提交上传文件
      submitFileForm() {
        this.$refs.upload.submit();
      },
      gcCodeSelect(e){
        console.log(e)
        // let result = this.gcCodeList.find(item => item.jtdm ==e)
        // e.factoryName = result.jtmc
        // e.factoryCode = result.jtdm
        e.factoryCode =  e.factoryName
      },
    }
  };
</script>
<style lang="scss" scoped>
  ::v-deep .cell .el-input__inner{
    border:none;
  }
</style>
